<?php
namespace app\mini\controller\commons;

use app\common\library\exception\ApiException;
use app\common\model\CarBrands;
use app\common\traits\InterfacesExceptionTrait;

class Amap extends Base
{
    use InterfacesExceptionTrait;

    /**
     * 获取所有城市
     * @return array
     * @throws \think\exception\DbException
     */
    public function citys()
    {
        $brands = \app\common\model\Amap::field('id,name,city_initial_letter letter,lng,lat')->where(['level'=>2])->select()->toArray();
        //把获取到首字母的数组排序
        $sort="SORT_ASC";
        $brands= arraySequence($brands, "letter",$sort);
        //创建新数组
        $data = [];
        //通过字段相同的分到一个新数组
        foreach($brands as &$v){
            $data[$v['letter']][]=$v;
        }
        return $this->response($data);
    }

    /**
     * 获取所有城市
     * @return array
     * @throws \think\exception\DbException
     */
    public function citys_new()
    {
        $result['hot_citys'] = \app\common\model\Amap::field('id,name,city_initial_letter letter,lng,lat')->where(['level'=>2,'is_hot'=>1])->select()->toArray();//热门城市
        $brands = \app\common\model\Amap::field('id,name,city_initial_letter letter,lng,lat')->where(['level'=>2])->select()->toArray();
        //把获取到首字母的数组排序
        $sort="SORT_ASC";
        $brands= arraySequence($brands, "letter",$sort);
        //创建新数组
        $data = [];
        //通过字段相同的分到一个新数组
        foreach($brands as &$v){
            $data[$v['letter']][]=$v;
        }
        $result['all_citys'] = $data;
        return $this->response($result);
    }

    /**
     * 城市搜索
     * @return array
     * @throws \think\exception\DbException
     */
    public function search_city()
    {
        $city_name = input('name','');
        if (empty($city_name))
            throw new ApiException('名称缺失',404,0);
        $data = \app\common\model\Amap::field('id,name,city_initial_letter letter,lng,lat')->where(['name'=>['like','%'.$city_name.'%']])->select()->toArray();
        return $this->response($data);
    }

    /**
     * 获取指定城市的县/区
     * @return array
     * @throws \think\exception\DbException
     */
    public function districts()
    {
        $city_id = input('city_id','');
        if (empty($city_id))
            throw new ApiException('城市信息未选择',404,0);
        $city = \app\common\model\Amap::get($city_id);
        $list = \app\common\model\Amap::field('id,name')->where(['level'=>3,'city'=>$city['city']])->select();
        return $this->response($list);
    }

    /**
     * 获取定位所在的城市信息
     * @return array
     * @throws \think\exception\DbException
     */
    public function get_city_info()
    {
        $lat = input('lat','');
        $lng = input('lng','');
        if (empty($lat) || empty($lng))
            throw new ApiException('参数缺失', 200);
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$lng}&key=".\think\Env::get('app.tencentkey');
        $info = http_request($url);
        $info_arr = json_decode($info,true);
        $city = \app\common\model\Amap::get(['name'=>$info_arr['result']['address_component']['city']]);
        return $this->response(['id'=>$city['id'],'name'=>$city['name'],'lat'=>$lat,'lng'=>$lng,'letter'=>$city['city_initial_letter']]);
    }

}